//
//  JHAssetsCollectionManager.m
//  JHAssetsPhotoPickerViewController
//
//  Created by admin on 15/11/13.
//  Copyright © 2015年 mohe. All rights reserved.
//

#import "JHAssetsCollectionManager.h"
#import "JHAssetsManager.h"

@implementation JHAssetsCollectionManager
+ (instancetype)defaultManager
{
    static JHAssetsCollectionManager *defaultManager = nil;
    static dispatch_once_t predicate;
    dispatch_once(&predicate, ^{
        defaultManager = [[self alloc]init];
    });
    return defaultManager;
}
+ (NSArray<PHAssetCollection *> *)loadCollections
{
    NSMutableArray *array = [NSMutableArray array];

    // 列出所有相册智能相册
    PHFetchResult *collectionResults = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeSmartAlbum subtype:PHAssetCollectionSubtypeAlbumRegular options:nil];
    [collectionResults enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [array addObject:obj];
    }];
    // 列出所有用户创建的相册
    PHFetchResult *topLevelUserCollections = [PHCollectionList fetchTopLevelUserCollectionsWithOptions:nil];
    [topLevelUserCollections enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [array addObject:obj];
    }];
    for(PHAssetCollection *collection in collectionResults){
        
        PHFetchResult *fetchResult = [PHAsset fetchAssetsInAssetCollection:collection options:nil];

        //已隐藏、慢动作、全景照片、连拍快照、延时摄影、最近删除、最近添加、视频
        if(collection.assetCollectionSubtype == PHAssetCollectionSubtypeSmartAlbumAllHidden ||
           collection.assetCollectionSubtype == PHAssetCollectionSubtypeSmartAlbumSlomoVideos ||
           collection.assetCollectionSubtype == PHAssetCollectionSubtypeSmartAlbumRecentlyAdded ||
           collection.assetCollectionSubtype == PHAssetCollectionSubtypeSmartAlbumPanoramas ||
           collection.assetCollectionSubtype == PHAssetCollectionSubtypeSmartAlbumBursts ||
           collection.assetCollectionSubtype == PHAssetCollectionSubtypeSmartAlbumTimelapses ||
           collection.assetCollectionSubtype == 1000000201 ||
           collection.assetCollectionSubtype == PHAssetCollectionSubtypeSmartAlbumVideos){
            [array removeObject:collection];
        }
        if(fetchResult.count == 0){
            [array removeObject:collection];
        }
    
    }
    return array;
}
- (PHAsset *)getCoverImageWithCollection:(PHAssetCollection *)collection
{
    PHFetchOptions *options = [[PHFetchOptions alloc] init];
    options.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:NO]];
    PHFetchResult *fetchResult = [PHAsset fetchAssetsInAssetCollection:collection options:options];
    if(fetchResult.count > 0){
        
        return [fetchResult firstObject];
    }
    return nil;
}
- (NSInteger)getCountOfCollection:(PHAssetCollection *)collection
{
    PHFetchResult *fetchResult = [PHAsset fetchAssetsInAssetCollection:collection options:nil];
    return fetchResult.count;
}
@end
